package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 根据分页查询频道
     */
    @Override
    public ResponseResult<List<AdChannel>> findChannelByPage(ChannelDto channelDto) {
        //判断数据是否合法
        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页数据检查，如果page或size数据为空或不合法，则给page初始值1，size初始值10
        channelDto.checkParam();
        //使用mybatis-plus执行查询
        Page page = new Page<>(channelDto.getPage(), channelDto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        //判断name是否为空
        if (StringUtils.isNotBlank(channelDto.getName())) {
            //模糊查询
            wrapper.like(AdChannel::getName, channelDto.getName());
        }
        IPage iPage = this.page(page, wrapper);
        //封装数据并返回
        return new PageResponseResult(channelDto.getPage(), channelDto.getSize(), iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 频道新增
     */
    @Transactional()
    @Override
    public ResponseResult addChannel(AdChannel adChannel) {
        //1判断数据是否合法，频道名称不能为空，频道名称不可重复
        //1.1 不能为空
        if (adChannel == null || !StringUtils.isNotBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2 不可重复
      /*  LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdChannel::getName, adChannel.getName());
        AdChannel channel = this.getOne(wrapper);*/
        AdChannel channel = findChannelByName(adChannel);
        if (channel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //数据合法，mybatis-plus执行添加操作
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return ResponseResult.okResult();
    }

    /**
     * 频道修改
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) throws Exception {
        //判断数据是否合法
        //不能为空
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //数据库中是否有该频道
        AdChannel channel = findChannelById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
        }
        //要修改的名字不能重复
        if (StringUtils.isBlank(adChannel.getName()) || (!channel.getName().equals(adChannel.getName()) && findChannelByName(adChannel) != null)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "名称为空或者当前名称以存在");
        }
        //调用mybatis-plus执行更新
        adChannel.setCreatedTime(new Date());
        this.updateById(adChannel);
        return ResponseResult.okResult();
    }

    /**
     * 根据id删除频道
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //判断数据是否合法
        AdChannel channel = findChannelById(id);
        if (id == null || channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE);
        }
        //mybatis-plus执行删除
        LambdaUpdateWrapper<AdChannel> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(AdChannel::getId, id);
        this.remove(wrapper);
        return ResponseResult.okResult();
    }

    /**
     * 根据name查询频道
     */
    private AdChannel findChannelByName(AdChannel adChannel) {
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdChannel::getName, adChannel.getName());
        return getOne(wrapper);
    }

    /**
     * 很久id查询channel
     */
    private AdChannel findChannelById(Integer id) {

        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdChannel::getId, id);
        return getOne(wrapper);

    }

    /**
     * 查询所有频道
     */
    @Override
    public ResponseResult findAllChannels() {
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery()
                .eq(AdChannel::getStatus, true)
                .orderByAsc(AdChannel::getOrd);
        List<AdChannel> list = list(wrapper);
        return ResponseResult.okResult(list);
    }
}
